
<template>
  <section>
    <!--工具条 - 高级查询框-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="query">
        <el-form-item>
          <el-input v-model="query.keyword" placeholder="请输入关键字"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="search">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onsale">上架</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="offsale">下架</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="pageInfo.rows" highlight-current-row v-loading="listLoading" @selection-change="selsChange" height="430" style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" fixed width="60">
      </el-table-column>
      <el-table-column prop="title" fixed label="名称" width="120" sortable>
      </el-table-column>
      <el-table-column prop="cover" label="封面图" width="120" sortable>
        <template slot-scope="scope">
          <img :src="scope.row.cover" width="30px" height="30px">
        </template>
      </el-table-column>
      <el-table-column prop="saleprice" label="原价" width="100" sortable>
      </el-table-column>
      <el-table-column prop="costprice" label="折扣价" width="100" sortable>
      </el-table-column>
      <el-table-column prop="isnew" label="是否新车" width="120" sortable>
        <template slot-scope="scope">
          <span v-if="scope.isnew == 1">是</span>
          <span v-else>否</span>
        </template>
      </el-table-column>
      <el-table-column prop="reigstertime" label="上牌时间" width="100" sortable>
      </el-table-column>
      <el-table-column prop="mileage" label="里程数" width="100" sortable>
      </el-table-column>
      <el-table-column prop="shopname" label="店铺名称" width="100" sortable>
      </el-table-column>
      <el-table-column prop="onsaletime" label="上架时间" width="100" sortable>
      </el-table-column>
      <el-table-column prop="offsaletime" label="下架时间" width="100" sortable>
      </el-table-column>
      <el-table-column prop="state" label="状态" min-width="180" sortable>
        <template slot-scope="scope">
          <span v-if="scope.row.state == 1" style="color: blue">上架</span>
          <span v-else>下架</span>
        </template>
      </el-table-column>
      <el-table-column prop="costeffective" label="超值" min-width="180" sortable>
        <template slot-scope="scope">
          <span v-if="scope.row.costeffective == 1">是</span>
          <span v-else>否</span>
        </template>
      </el-table-column>
      <el-table-column prop="rushsale" label="急售" min-width="180" sortable>
        <template slot-scope="scope">
          <span v-if="scope.row.rushsale == 1">是</span>
          <span v-else>否</span>
        </template>
      </el-table-column>
      <el-table-column prop="quasinewcar" label="准新车" min-width="180" sortable>
        <template slot-scope="scope">
          <span v-if="scope.row.quasinewcar == 1">是</span>
          <span v-else>否</span>
        </template>
      </el-table-column>
      <el-table-column prop="transitivecountry" label="可提全国" min-width="180" sortable>
        <template slot-scope="scope">
          <span v-if="scope.row.transitivecountry == 1">是</span>
          <span v-else>否</span>
        </template>
      </el-table-column>
      <el-table-column prop="type.name" label="类型" width="100" sortable>
      </el-table-column>
      <el-table-column prop="seller.username" label="营销人员" width="100" sortable>
      </el-table-column>
      <el-table-column prop="auditstate" label="审核状态" width="100" sortable>
        <template slot-scope="scope">
          <span v-if="scope.row.auditstate == 0">待审核</span>
          <span v-if="scope.row.auditstate == 1">审核成功</span>
          <span v-if="scope.row.auditstate == 2">审核驳回</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" width="150">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
          <el-button size="small" @click="handleResources(scope.$index, scope.row)">资源维护</el-button>
          <el-button size="small" @click="handleDel(scope.$index, scope.row)">检测维护</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条 - 批量删除&分页条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination
          layout="prev, pager, next"
          :current-page="query.currentPage"
          @current-change="handleCurrentChange"
          :page-size="query.pageSize"
          :total="pageInfo.total"
          style="float:right;">
      </el-pagination>
    </el-col>

    <!--新增界面-->
    <el-dialog title="新增/修改" :visible.sync="carFormVisible" :close-on-click-modal="false">
      <el-form :model="carForm" label-width="80px" :rules="carFormRules" ref="carForm">
        <el-form-item label="名称">
          <el-input v-model="carForm.title" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="封面">
          <el-upload
              class="upload-demo"
              action="http://localhost:80/fastdfs/"
              :on-preview="coverHandlePreview"
              :on-remove="coverHandleRemove"
              :on-success="coverHandleSuccess"
              :file-list="coverFileList"
              list-type="picture">
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
        </el-form-item>
        <el-form-item label="原价">
          <el-input v-model="carForm.saleprice" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="售价">
          <el-input v-model="carForm.costprice" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="是否新车">
          <el-radio-group v-model="carForm.isnew">
            <el-radio class="radio" :label="1">是</el-radio>
            <el-radio class="radio" :label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="上牌时间">
          <el-date-picker
              v-model="carForm.reigstertime"
              type="date"
              placeholder="请选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="里程数">
          <el-input v-model="carForm.mileage" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="超值">
          <el-radio-group v-model="carForm.costeffective">
            <el-radio class="radio" :label="1">是</el-radio>
            <el-radio class="radio" :label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="急售">
          <el-radio-group v-model="carForm.rushsale">
            <el-radio class="radio" :label="1">是</el-radio>
            <el-radio class="radio" :label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="准新车">
          <el-radio-group v-model="carForm.quasinewcar">
            <el-radio class="radio" :label="1">是</el-radio>
            <el-radio class="radio" :label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="可迁全国">
          <el-radio-group v-model="carForm.transitivecountry">
            <el-radio class="radio" :label="1">是</el-radio>
            <el-radio class="radio" :label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="车辆类型">
          <el-cascader v-model="carForm.type"
                       :options="carTypeTree"
                       :props="{
							   checkStrictly: true,
							   label:'name',
							   value:'id'
							}"
                       clearable></el-cascader>
        </el-form-item>
        <el-form-item label="基本信息">
          <quill-editor v-model="carForm.carDetail.info" :options="quillOption">
          </quill-editor>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="carFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="save" :loading="carFormLoading">提交</el-button>
      </div>
    </el-dialog>


    <el-dialog title="资源维护" :visible.sync="carResourcesFormVisible" :close-on-click-modal="false">
      <el-form :model="carResourcesForm" label-width="80px" ref="carForm">
        <el-form-item label="主图">
          <el-upload
              class="upload-demo"
              action="http://localhost:80/fastdfs/"
              :on-preview="imgHandlePreview"
              :on-remove="imgHandleRemove"
              :on-success="imgHandleSuccess"
              :file-list="imgFileList"
              list-type="picture">
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
        </el-form-item>
        <el-form-item label="副图">
          <el-upload
              class="upload-demo"
              action="http://localhost:80/fastdfs/"
              :on-preview="viceimgHandlePreview"
              :on-remove="viceimgHandleRemove"
              :on-success="viceimgHandleSuccess"
              :file-list="viceimgFileList"
              list-type="picture">
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
        </el-form-item>
        <el-form-item label="更多图片">
          <el-upload
              class="upload-demo"
              action="http://localhost:80/fastdfs/"
              :on-preview="moreimgHandlePreview"
              :on-remove="moreimgHandleRemove"
              :on-success="moreimgHandleSuccess"
              :file-list="moreimgFileList"
              list-type="picture">
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="carFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="saveCarResources" :loading="carFormLoading">提交</el-button>
      </div>
    </el-dialog>
  </section>
</template>

<script>
import {quillEditor} from "vue-quill-editor"; //调用编辑器
import 'quill/dist/quill.core.css';
import 'quill/dist/quill.snow.css';
import 'quill/dist/quill.bubble.css';
import quillConfig from "../../../common/js/quill-config.js"
export default {
  components: {
    quillEditor
  },
  data() {
    return {
      quillOption:quillConfig,
      query:{
        currentPage:1,  // 当前页
        pageSize:10,  // 每页显示条数
        keyword:""  // 关键字
      },
      pageInfo:{
        total:0,
        rows:[]
      },
      listLoading: false,
      sels: [],//列表选中列

      carFormVisible: false,//新增修改界面是否显示
      carFormLoading: false,
      carFormRules: {
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ]
      },

      coverFileList: [],
      carTypeTree:[],

      //新增修改界面数据
      carForm: {
        id:null,
        title:"",
        cover:"",
        saleprice:0,
        costprice:0,
        isnew:0,
        reigstertime:"",
        mileage:null,
        costeffective:0,
        rushsale:0,
        quasinewcar:0,
        transitivecountry:0,
        type:{
          id:null,
          name:""
        },
        carDetail:{
          info:""
        }
      },

      imgFileList: [],
      viceimgFileList: [],
      moreimgFileList: [],
      carResourcesFormVisible:false,
      carResourcesForm:{
        carId:null,
        img:"",
        viceimg:"",
        moreimg:""
      }
    }
  },
  methods: {
    onsale(){
      var ids = this.sels.map(item => item.id);
      //获取选中的行
      if(!this.sels || this.sels.length  <1){
        this.$message({ message: '老铁，你不选中数据，臣妾上架不了啊....',type: 'error'});
        return;
      }
      this.$confirm('确认上架选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        this.$http.post('/car/onsale',ids).then((res) => {
          this.listLoading = false;
          if(res.data.success){
            this.$message({
              message: '上架成功',
              type: 'success'
            });
            this.getCars();
          }else{
            this.$message({
              message: res.data.message,
              type: 'error'
            });
          }
        });
      }).catch(() => {

      });
    },
    offsale(){
      var ids = this.sels.map(item => item.id);
      //获取选中的行
      if(!this.sels || this.sels.length  <1){
        this.$message({ message: '老铁，你不选中数据，臣妾下架不了啊....',type: 'error'});
        return;
      }
      this.$confirm('确认下架选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        this.$http.post('/car/offsale',ids).then((res) => {
          this.listLoading = false;
          if(res.data.success){
            this.$message({
              message: '下架成功',
              type: 'success'
            });
            this.getCars();
          }else{
            this.$message({
              message: res.data.message,
              type: 'error'
            });
          }
        });
      }).catch(() => {
      });
    },
    saveCarResources(){
      this.$http.put("/carResources",this.carResourcesForm)
          .then(result => {
            result = result.data;
            if(result.success){
              this.$message({message: '保存成功', type: 'success'});
              this.getCars();
              this.carResourcesFormVisible = false;
            }else{
              this.$message({message: '保存失败!'+result.message, type: 'error'});
            }
          })
    },
    handleResources(index,row){
      this.carResourcesFormVisible = true;
      this.carResourcesForm.carId = row.id; // 车辆资源的carId就是当前行的id
      this.imgFileList = [];
      this.viceimgFileList = [];
      this.moreimgFileList = []
    },
    getTreeData(){
      // 发送一个异步请求: get请求 /product/carType/treeData
      this.$http.get("/carType/tree").then(res=>{
        console.log(this);
        this.carTypeTree = res.data.result;
      });
    },
    //文件上传成功回调
    moreimgHandleSuccess(response, file, fileList){ // fileList 是装的当前文件上传组件上传的文件的地址
      console.log(response,file,fileList)
      let imgStr = "";
      for(let i=0;i<fileList.length;i++){ // 如果是多张图片,就拼接起来,中间用逗号分隔,但是如果是最后一张图片,逗号就不用拼接
        if(i == fileList.length-1){
          imgStr = imgStr+fileList[i].response.result;
        }else{
          imgStr = imgStr+fileList[i].response.result+",";
        }
      }
      this.carResourcesForm.moreimg = imgStr;
    },
    //文件删除
    moreimgHandleRemove(file, fileList) {
      console.log(file,fileList)
      var filePath =file.response.result;
      this.$http.delete("/fastdfs/?path="+filePath) // 这个地方不用uri传参的方式,因为图片路径里面包含/,到时候会分不清谁是谁的,因此采用?传参的方式
          .then(res=>{
            if(res.data.success){
              // 删除成功以后,要删除掉this.carResourcesForm.img的值
              this.$message({
                message: '删除成功!',
                type: 'success'
              });
            }else{
              this.$message({
                message: '删除失败!',
                type: 'error'
              });
            }
          })
    },
    //图片预览
    moreimgHandlePreview(file) {
      console.log(file);
    },
    //文件上传成功回调
    viceimgHandleSuccess(response, file, fileList){ // fileList 是装的当前文件上传组件上传的文件的地址
      console.log(response,file,fileList)
      let imgStr = "";
      for(let i=0;i<fileList.length;i++){ // 如果是多张图片,就拼接起来,中间用逗号分隔,但是如果是最后一张图片,逗号就不用拼接
        if(i == fileList.length-1){
          imgStr = imgStr+fileList[i].response.result;
        }else{
          imgStr = imgStr+fileList[i].response.result+",";
        }
      }
      this.carResourcesForm.viceimg = imgStr;
    },
    //文件删除
    viceimgHandleRemove(file, fileList) {
      console.log(file,fileList)
      var filePath =file.response.result;
      this.$http.delete("/fastdfs/?path="+filePath) // 这个地方不用uri传参的方式,因为图片路径里面包含/,到时候会分不清谁是谁的,因此采用?传参的方式
          .then(res=>{
            if(res.data.success){
              // 删除成功以后,要删除掉this.carResourcesForm.img的值
              this.$message({
                message: '删除成功!',
                type: 'success'
              });
            }else{
              this.$message({
                message: '删除失败!',
                type: 'error'
              });
            }
          })
    },
    //图片预览
    viceimgHandlePreview(file) {
      console.log(file);
    },
    //文件上传成功回调
    imgHandleSuccess(response, file, fileList){ // fileList 是装的当前文件上传组件上传的文件的地址
      console.log(response,file,fileList)
      let imgStr = "";
      for(let i=0;i<fileList.length;i++){ // 如果是多张图片,就拼接起来,中间用逗号分隔,但是如果是最后一张图片,逗号就不用拼接
        if(i == fileList.length-1){
          imgStr = imgStr+fileList[i].response.result;
        }else{
          imgStr = imgStr+fileList[i].response.result+",";
        }
      }
      this.carResourcesForm.img = imgStr;
    },
    //文件删除
    imgHandleRemove(file, fileList) {
      console.log(file,fileList)
      var filePath =file.response.result;
      this.$http.delete("/fastdfs/?path="+filePath) // 这个地方不用uri传参的方式,因为图片路径里面包含/,到时候会分不清谁是谁的,因此采用?传参的方式
          .then(res=>{
            if(res.data.success){
              // 删除成功以后,要删除掉this.carResourcesForm.img的值
              this.$message({
                message: '删除成功!',
                type: 'success'
              });
            }else{
              this.$message({
                message: '删除失败!',
                type: 'error'
              });
            }
          })
    },
    //图片预览
    imgHandlePreview(file) {
      console.log(file);
    },
    //文件上传成功回调
    coverHandleSuccess(response, file, fileList){
      console.log(response,file,fileList)
      this.carForm.cover = response.result;
    },
    //文件删除
    coverHandleRemove(file, fileList) {
      console.log(file,fileList)
      var filePath =file.response.result;
      this.$http.delete("/fastdfs/?path="+filePath) // 这个地方不用uri传参的方式,因为图片路径里面包含/,到时候会分不清谁是谁的,因此采用?传参的方式
          .then(res=>{
            if(res.data.success){
              // 删除成功以后,要删除掉this.carForm.cover的值
              this.carForm.cover = "";
              this.$message({
                message: '删除成功!',
                type: 'success'
              });
            }else{
              this.$message({
                message: '删除失败!',
                type: 'error'
              });
            }
          })
    },
    //图片预览
    coverHandlePreview(file) {
      console.log(file);
    },
    search(){
      this.query.currentPage = 1;
      this.getCars();
    },
    handleCurrentChange(val) {  // val是当前页
      this.query.currentPage = val;
      this.getCars();
    },
    //获取部门列表
    getCars() {
      debugger
      this.listLoading = true;  // 弹出加载框
      this.$http.post("/car",this.query)   // axios.defaults.baseURL+/car  http://localhost:80/api/car
          .then(result => {
            result = result.data; // result.data  {success:true,message:"",result:{total:1,rows:[]}}
            console.log(result)
            if(result.success){
              this.pageInfo = result.result;
              this.listLoading = false;  // 关闭加载框
            }else{
              this.$message({message: '查询分页失败!'+result.message, type: 'error'});
            }
          })
          .catch(result => {
            this.$message({message: "查询分页失败!", type: 'error'});
          })
    },
    //删除
    handleDel: function (index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        this.$http.delete("/car/"+row.id)
            .then(result => {
              result = result.data;
              if(result.success){
                this.$message({message: '删除成功', type: 'success'});
                // 刷新列表
                this.query.currentPage = 1;
                this.getCars();
                this.listLoading = false;  // 关闭弹出框
              }else{
                this.$message({message: '删除失败!'+result.message, type: 'error'});
              }
            })
      }).catch(() => {
        this.$message({message: '已取消删除', type: 'info'});
      });
    },
    //显示编辑界面
    handleEdit: function (index, row) {
      this.carFormVisible = true;
      this.carForm = Object.assign({}, row);
      this.getTreeData();
    },
    //显示新增界面
    handleAdd: function () {
      this.carFormVisible = true;
      this.carForm = {
        id:null,
        title:"",
        cover:"",
        saleprice:0,
        costprice:0,
        isnew:0,
        reigstertime:"",
        mileage:null,
        costeffective:0,
        rushsale:0,
        quasinewcar:0,
        transitivecountry:0,
        type:{
          id:null,
          name:""
        },
        carDetail:{
          info:""
        }
      };
      this.getTreeData();
    },
    //新增修改保存方法
    save: function () {
      console.log(this.carForm)
      this.$refs.carForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.carFormLoading = true;
            let para = Object.assign({}, this.carForm);
            // 级联下拉框绑定后的值是数组,但是后端的parent需要的是一个对象,所以需要我们手动转一下
            if(para.type != null){
              let array = para.type;
              // 当用户选择多级部门以后,对于该部门而言,最后一个部门id才是它的父部门
              para.type = {id:array[array.length-1]};
            }
            this.$http.put("/car",para)
                .then(result => {
                  result = result.data;
                  if(result.success){
                    this.$message({message: '保存成功', type: 'success'});
                    this.carFormLoading = false;
                    this.query.currentPage = 1;
                    this.getCars();
                    this.carFormVisible = false;
                  }else{
                    this.$message({message: '保存失败!'+result.message, type: 'error'});
                  }
                }).catch(result => {
              this.$message({message: '保存失败!', type: 'error'});
            })
          });
        }
      });
    },
    selsChange: function (sels) {
      this.sels = sels;
    },
    //批量删除
    batchRemove: function () {
      var ids = this.sels.map(item => item.id).toString();
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        //NProgress.start();
        let para = { ids: ids };
        batchRemoveUser(para).then((res) => {
          this.listLoading = false;
          //NProgress.done();
          this.$message({
            message: '删除成功',
            type: 'success'
          });
          this.getCars();
        });
      }).catch(() => {

      });
    }
  },
  mounted() {
    debugger
    this.getCars();  // 做分页查询
  }
}

</script>

<style scoped>

</style>